package com.csi.controller.user;

import com.alibaba.fastjson2.JSON;
import com.csi.domain.User;
import com.csi.pojo.Result;
import com.csi.service.UserService;
import com.csi.service.impl.UserServiceImpl;
import com.csi.utils.JWTUtils;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.http.HttpClient;

@WebServlet("/user/sendMessageAfterController")
public class SendMessageAfterController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("application/json;charset=utf-8");
        Result result = new Result<>();

        String phone = req.getParameter("phone");
        String inputCode = req.getParameter("code");

        // 从 application 中获取之前生成的验证码
        ServletContext application = req.getServletContext();
        VerifyCode verifyCode = (VerifyCode) application.getAttribute(phone);
        // System.out.println("application中的验证码：" + verifyCode.getCode());
        // System.out.println("用户输入的验证码：" + inputCode);

        if (verifyCode == null) {
            // 验证码已过期或不存在
            result.setCode(7002);
            result.setMsg("验证码已过期或不存在");
        } else if (!verifyCode.getCode().equals(inputCode)) {
            // 验证码不正确
            result.setCode(7003);
            result.setMsg("验证码不正确");
        } else if (System.currentTimeMillis() > verifyCode.getExpireTime()) {
            // 验证码已过期
            result.setCode(7002);
            result.setMsg("验证码已过期或不存在");
        } else {
            UserService userService = new UserServiceImpl();

            Integer userId = userService.queryUserIdByPhone(phone);
            User user = userService.findById(userId);

            String token = JWTUtils.createJWT(1000 * 60 * 60 * 3, user.getUserId(), user.getRoles(),user.getUserNumber());
            user.setToken(token);
            String longToken = JWTUtils.createJWT(1000 * 60 * 60 * 24, user.getUserId(), user.getRoles(),user.getUserNumber());
            user.setLongToken(longToken);
            // 验证码正确，清除之前保存的验证码
            application.removeAttribute(phone);
            // 验证通过
            result.setCode(200);
            result.setMsg("success");
            result.setDatas(user);


        }

        PrintWriter out = resp.getWriter();
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }
}

class VerifyCode {
    private String code;
    private long expireTime;

    public VerifyCode(String code, long expireTime) {
        this.code = code;
        this.expireTime = expireTime;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public long getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(long expireTime) {
        this.expireTime = expireTime;
    }
}
